package com.msmall.community.job;

import java.util.List;

import com.msmall.base.cache.CacheFactory;
import com.msmall.base.util.Constants;
import com.msmall.community.bbs.model.Topic;
import com.msmall.community.bbs.service.TopicManager;

public class BBSJob {

	private TopicManager topicManager;
	/**
	 * 每隔1小时刷新所有版区的前50个点击率最高的帖子，并且是当天发布的，放入缓存
	 */
	public void initHotTopic(){
		List<Topic> topics = topicManager.initHotTopic();
		if(topics.size() < 20){
			List<Topic> oldtopics = (List)CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_BBS_HOT_TOPIC_LIST);
			if(oldtopics != null && oldtopics.size() > 0){
				int need = oldtopics.size() > 20 - topics.size()?20-topics.size():oldtopics.size();
				topics.addAll(oldtopics.subList(oldtopics.size()-need, oldtopics.size()));
			}
		}
		CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).put(Constants.CACHE_KEY_BBS_HOT_TOPIC_LIST, topics);
	}

	public void setTopicManager(TopicManager topicManager) {
		this.topicManager = topicManager;
	}
}
